package com.xunkids.bos.migration.component.transmission.pojo;


import cn.hutool.core.util.ObjectUtil;
import com.xunkids.bos.migration.dto.bos.entity.BosEventBaseDto;
import com.xunkids.bos.migration.dto.bos.entity.BosEventDto;

import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * 请求消息
 *
 * @author xigexb
 * @version 1.0.0
 * @since 2022/8/26 17:08
 */
public class RequestMessage implements TransportMessage<Map<String, Object>> {


    public String getId() {
        return id;
    }

    public String getCode() {
        return code;
    }

    public String getTopic() {
        return topic;
    }

    public BosEventBaseDto getData() {
        return data;
    }

    public AtomicBoolean getState() {
        return state;
    }

    /**
     * ID
     */
    private final String id;

    /**
     * code
     */
    private final String code;

    /**
     * topic
     */
    private final String topic;

    /**
     * 数据
     */
    private final BosEventBaseDto data;

    private final AtomicBoolean state = new AtomicBoolean(true);

    /**
     * 请求/响应代码
     *
     * @return code
     */
    @Override
    public String code() {
        return code;
    }

    /**
     * 消息ID
     *
     * @return id
     */
    @Override
    public String id() {
        return id;
    }

    /**
     * 数据
     *
     * @return T data
     */
    @Override
    public Optional<BosEventBaseDto> data() {
        return Optional.ofNullable(data);
    }

    /**
     * 主题
     *
     * @return 主题
     */
    @Override
    public String topic() {
        return this.topic;
    }

    /**
     * 请求状态
     * true 为请求
     * false 为响应
     *
     * @return true
     */
    @Override
    public boolean state() {
        return state.get();
    }

    public RequestMessage(String id, String code, String topic, BosEventBaseDto data) {
        this.id = id;
        this.code = code;
        this.topic = topic;
        this.data = ObjectUtil.isNotEmpty(data)?  data : new BosEventBaseDto();
    }

}
